(function(parentObj, parentNames) {
    var TreeList = parentObj.create(parentNames);
    TreeList.init = function(treeId,setting,zNodes){
        var params = {
            check: {
                chkStyle: "radio"
            }             
        };
        jq.extend(params,setting);

        var createNodes = function(obj,level,root){
            for (var i=0;i<obj.length;i++) {

                if (!root){nodesHtml += '<li>';}

                nodesHtml += '<div class="supp-list-tree-title" data-target-id="target_'+obj[i].id+'">'

                if (obj[i].children == undefined) {//判断是否有下一级控制箭头是否显示
                    nodesHtml += '<span class="supp-arrow-left-lg" style="background:url();"></span>';
                } else {
                    nodesHtml += '<span class="supp-arrow-left-lg"></span>';
                }
                               
                nodesHtml += '<div class="supp-list-tree-oper">'
                        +'<input type="'+params.check.chkStyle+'" id="'+obj[i].id+'" name="dept_'+treeId.slice(1)+'" value="'+obj[i].id+'">'
                        +'<label for="'+obj[i].id+'" class="icon-label">'+obj[i].name+'</label>'
                        +'</div>'
                        +'</div>';

                var childrenLevel = level;
                if (obj[i].children != undefined) {
                    nodesHtml += '<ul class="supp-list-tree-wrapper supp-list-tree-wrapper-level-'+childrenLevel+'" id="target_'+obj[i].id+'">';
                    createNodes(obj[i].children,++childrenLevel,false);
                    nodesHtml += "</ul>";
                }

                if (!root){nodesHtml += "</li>";}
            }
            return nodesHtml;
            
        }
        
        var bindEvent = function(){ 
            jq(".supp-arrow-left-lg").each(function(i,n){
                n.addEventListener("tap",function(){
                    var targetId = jq(n).parent().attr("data-target-id");
                    var targetListCon = jq("#" + targetId);
                    if (targetListCon.css("display") == "none") {
                        targetListCon.show();
                        jq(n).addClass("supp-arrow-bottom-lg");
                    } else {
                        targetListCon.hide();
                        jq(n).removeClass("supp-arrow-bottom-lg");
                    }                
                });
            });         
        }   

        jq(treeId).html("");
        var nodesHtml = "",$form = jq("<form>");
        $form.html(createNodes(zNodes,1,true));
        jq(treeId).append($form);
        bindEvent();  
    }
    
    TreeList.transferDate = function(sNodes) {
        var i,l,
        key = "id",
        parentKey = "pId",
        childKey = "children";
        if (!key || key=="" || !sNodes) return [];

        if ($.isArray(sNodes)) {
            var r = [];
            var tmpMap = [];
            for (i=0, l=sNodes.length; i<l; i++) {
                tmpMap[sNodes[i][key]] = sNodes[i];
            }
            for (i=0, l=sNodes.length; i<l; i++) {
                if (tmpMap[sNodes[i][parentKey]] && sNodes[i][key] != sNodes[i][parentKey]) {
                    if (!tmpMap[sNodes[i][parentKey]][childKey])
                        tmpMap[sNodes[i][parentKey]][childKey] = [];
                    tmpMap[sNodes[i][parentKey]][childKey].push(sNodes[i]);
                } else {
                    r.push(sNodes[i]);
                }
            }
            return r;
        }else {
            return [sNodes];
        }
    }
    //全部展开
    TreeList.showAll = function(){
         jq("#"+treeId).find(".supp-list-tree-wrapper").show();
         jq("#"+treeId).find(".supp-arrow-left-lg").addClass(" supp-arrow-bottom-lg");
    }

    //全部收起
    TreeList.hideAll = function(){
         jq("#"+treeId).find(".supp-list-tree-wrapper").hide();
         jq("#"+treeId).find(".supp-arrow-left-lg").removeClass(" supp-arrow-bottom-lg");
    }

    //重置
    TreeList.resetList = function(){
        var $form = jq("#"+treeId).find("form");
        $form[0].reset();
    }

    //确定
    TreeList.getList = function(){
        var $form = jq("#"+treeId).find("form"),chk_value=[];
        $form.find("input:checked").each(function(){
            chk_value.push({
                value : $(this).val(),
                text : $(this).next().html()
            }); 
        });
        return chk_value;
    }
})(Supp.Base,"Supp.TreeList");


